import Axios from "axios"
import { getData, delData } from 'utils/local'
import { message  } from 'antd'
const request = Axios.create({
  // baseURL: "/api",
  timeout: 20000,
});

const debounse = function (fnc, delay) {
  let timer = null;
  return function () {
    clearInterval(timer);
    timer = setTimeout(() => fnc(), delay);
  };
};

request.interceptors.request.use( config => {
  const accessPaths = ['login']
  /* 登录不需要权限 */
  const pass = accessPaths.some( path => new RegExp(path).test(config.url))
  if (!pass) {
    config.headers['authorization'] = getData('token')
  }
  return config
})

const lastMsg = debounse(function () {
  message.error("登录过期，重新登录");
  setTimeout(() =>  window.location.reload(), 800)
  delData("token");
}, 200);

request.interceptors.response.use(function (config) {
  if (config.data.code === "1004" || config.data.code === "10022") {
    lastMsg();
  }
  return config;
});

export default request